package com.le.tester.book.flightjava8.stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * createTime：2025-07-17 10:40
 * description：使用流遍历，流只能消费一次
 */
public class IteratorStream {
    public static void main(String[] args) {
        List<String> title = Arrays.asList("java8", "In", "Action");
        Stream<String> s = title.stream();

        s.forEach(System.out::println);
//        提示报错信息stream has already been operated upon or closed
        s.forEach(System.out::println);

        ArrayList<String> names = new ArrayList<>();
        ArrayList<Dish> menus = new ArrayList<>();
        for (Dish d : menus) {
            names.add(d.getName());
        }

//        使用显示迭代的话，一定是iterator
//        获取迭代器
        ArrayList<Dish> nameDish = new ArrayList<>();
        Iterator<Dish> iterator = menus.iterator();
        while (iterator.hasNext()) {
            Dish next = iterator.next();
            nameDish.add(next);
        }

//        流的内部迭代
        List<String> menusNames = menus.stream()
                .map(Dish::getName)
                .collect(Collectors.toList());


        ArrayList<Dish> dishes = new ArrayList<>();
        dishes.stream()
                .filter(d -> d.getCalories() > 300)
                .distinct()
                .limit(3)
                .count();
    }
}
